<template>
  <div id="home">
    <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
    <tab-control class="tab-control" 
                   :titles="['流行', '新款', '精选']" 
                   ref="tabControlFixed" 
                   @tabClick="tabClick"
                   v-show="isTabFixed" />
    <scroll class="scroll-area" 
            ref="scroll" 
            :probe-type="3" 
            :pull-up-load="true"
            @scroll="contentScroll" 
            @pullingUp="loadMore">

      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad" />
      <home-recommend :recommends="recommends" />
      <home-feature />
      <tab-control class="tab-control" 
                   :titles="['流行', '新款', '精选']" 
                   ref="tabControl" 
                   @tabClick="tabClick" />
      <goods-list :goods="showGoods" />
    </scroll>
    <back-top @click.native="backClick" v-show="isShowBackTop" />
  </div>
</template>

<script>
  import NavBar from "components/common/navbar/NavBar";
  import TabControl from "components/content/tabcontrol/TabControl";
  import GoodsList from "components/content/goods/GoodsList";
  import Scroll from "components/common/scroll/Scroll";

  import HomeSwiper from "./childComps/HomeSwiper";
  import HomeRecommend from "./childComps/HomeRecommend";
  import HomeFeature from "./childComps/HomeFeature";

  import {getHomeMultiData, getHomeGoods} from "network/home";
  import {imgListenerMixin, backTopListenerMixin} from "common/mixin";

  export default {
    name: "Home",
    components: {
      NavBar,
      HomeSwiper,
      HomeRecommend,
      HomeFeature,
      TabControl,
      GoodsList,
      Scroll
    },
    data() {
      return {
        banners: [],
        recommends: [],
        goods: {
          pop: {page: 0, list: []},
          new: {page: 0, list: []},
          sell: {page: 0, list: []},
        },
        currentType: "pop",
        tabOffsetTop: 0,
        isTabFixed: false,
        saveY: 0
      }
    },
    // 重复代码的混入
    mixins: [imgListenerMixin, backTopListenerMixin],
    created() {
      // 1.请求多个数据
      this.getHomeMultiData();

      // 2.请求商品数据
      this.getHomeGoods('pop');
      this.getHomeGoods('new');
      this.getHomeGoods('sell');
    },
    activated() {
      this.$refs.scroll.scrollTo(0, this.saveY, 0);
      this.$refs.scroll.refresh();
    },
    deactivated() {
      // 1.保存Y值
      console.log("scrollY: ", this.$refs.scroll.getScrollY());
      this.saveY = this.$refs.scroll.getScrollY();
      // 2.取消全局事件的监听
      this.$bus.$off('imageLoad', this.imgListener);
    },
    computed: {
      showGoods() {
        return this.goods[this.currentType].list;
      }
    },
    methods: {
      // 事件监听相关方法
      tabClick(index) {
        console.log("index: ", index);
        switch(index) {
          case 0:
            this.currentType = "pop";
            break;
          case 1:
            this.currentType = "new";
            break;
          default:
            this.currentType = "sell";
        }
        this.$refs.tabControl.currentIndex = index;
        this.$refs.tabControlFixed.currentIndex = index;
      },
      contentScroll(position) {
        // console.log("position: ", position);
        // 1.判断BackTop是否显示
        this.backTopListener(position);

        // 2.决定TabControl是否吸顶
        this.isTabFixed = (-position.y) > this.tabOffsetTop;
      },
      loadMore() {
        console.log("上拉加载更多");
        this.getHomeGoods(this.currentType);
      },
      swiperImageLoad() {
        // 获取tabControl的offsetTop
        console.log("offsetTop: ", this.$refs.tabControl.$el.offsetTop);
        this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop;
      },
      // 网络请求相关方法
      getHomeMultiData() {
        getHomeMultiData().then(res => {
          // console.log(res);
          this.banners = res.data.banner.list;
          this.recommends = res.data.recommend.list;
        });
      },
      getHomeGoods(type) {
        const page = this.goods[type].page + 1;
        getHomeGoods(type, page).then(res => {
          // console.log(res);
          this.goods[type].list.push(...res.data.list);
          this.goods[type].page += 1;

          this.$refs.scroll.finishPullUp();
        });
      }
    }
  }
</script>

<style scoped>
  #home {
    position: relative;
    height: 100vh;
  }

  .home-nav {
    color: #fff;
    background-color: var(--color-tint);
  }

  .tab-control {
    position: relative;
    z-index: 10;
  }

  .scroll-area {
    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
    overflow: hidden;
  }
</style>
